<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Debug;
use EasyWeChat\Factory;
use app\models\{Withdraw, Agent};
use think\Db;
use think\facade\Log;

class TransMoney extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('TransMoney')->setDescription('微信转账');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        ini_set('max_execution_time', 0);
        //优惠券过期检测
        (new Withdraw())->where(['status' => 1, 'type' => 2])->chunk(10, function ($lists) {
            $config = config('wechat1.');
            $app = Factory::payment($config);
            foreach ($lists as $list) {
                $openid = (new Agent())->where('id', $list['user_id'])->value('openid') ?? '';
                $money = floor(($list['money'] - $list['fee'])*100);
                $res = $app->transfer->toBalance([
                    'partner_trade_no' => $list['id'], // 商户订单号，需保持唯一性(只能是字母或者数字，不能包含有符号)
                    'openid' => $openid,
                    'check_name' => 'FORCE_CHECK', // NO_CHECK：不校验真实姓名, FORCE_CHECK：强校验真实姓名
                    're_user_name' => $list['name'], // 如果 check_name 设置为FORCE_CHECK，则必填用户真实姓名
                    'amount' => $money, // 企业付款金额，单位为分
                    'desc' => '提现打款', // 企业付款操作说明信息。必填
                ]);
                dump($res);
            }
        });
    }
}
